<template>
  <div class="home-container">
    <!-- 顶部导航栏组件 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 频道列表 -->
    <!-- v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签 -->
    <!-- swipeable 属性可以开启滑动切换标签页 -->
    <!-- animated 属性可以开启切换标签内容时的转场动画 -->
    <van-tabs class="channel-tabs" v-model="active" swipeable animated>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <!-- 文章列表动态渲染 -->
        <article-list ref="article-list" :channel="channel" />
      </van-tab>
      <!-- 汉堡按钮 -->
      <!-- 解决最后一个标签被遮挡的问题 -->
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="ifont icgengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon="close"
      close-icon-position="top-right"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 在弹出层中使用频道编辑组件 -->
      <channel-edit
        :my-channels="channels"
        :active="active"
        @update-active="updateActive"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list'
// 加载注册频道编辑组件
import ChannelEdit from './components/channel-edit'
// 导入 vuex 中的 mapState 方法用于判断用户是否登录
import { mapState } from 'vuex'
// 导入本地存储模块中的的 Item 方法， 用于判断用户是否登录
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  computed: {
    ...mapState(['user'])
  },
  data () {
    return {
      active: 0,
      // 频道列表
      channels: [],
      // 控制编辑频道弹出层的显示状态
      isChannelEditShow: false
    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      // try {
      //   const { data } = await getUserChannels()
      //   this.channels = data.data.channels
      // } catch (err) {
      //   this.$toast('获取频道数据失败')
      // }
      let channels = []

      if (this.user) {
        // 已登录，请求获取用户频道列表
        const { data } = await getUserChannels()
        channels = data.data.channels
      } else {
        // 未登录，判断是否有本地的频道列表数据
        const localChannels = getItem('TOUTIAO_CHANNELS')
        // 如果存在本地的频道列表数据
        if (localChannels) {
          channels = localChannels
        } else {
          // 没有，请求获取默认频道列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        }
      }

      this.channels = channels
    },
    updateActive (index, isChannelEditShow = true) {
      this.active = index
      this.isChannelEditShow = isChannelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}
// 频道列表样式
/deep/ .channel-tabs {
  .van-tabs__wrap {
    height: 82px;
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
  }

  .van-tab {
    min-width: 200px;
    border-right: 1px solid #edeff3;
    font-size: 25px;
    color: #777777;
  }

  .van-tab--active {
    color: #333333;
  }

  .van-tabs__nav {
    padding-bottom: 0;
  }

  .van-tabs__line {
    bottom: 8px;
    width: 45px !important;
    height: 8px;
    background-color: #3296fa;
  }
}
// 汉堡按钮样式
.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;

  i.toutiao {
    font-size: 33px;
  }
  &:before {
    content: '';
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
}
</style>
